package com.infore.water.dao;

import java.util.List;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;


@Repository
public class StationDao {
  @Autowired
  private JdbcTemplate jdbcTemplate;
  
  public List<Map<String, Object>> queryStationByRoleId(String roleId) {
    String sql = "select a.id stationId,a.name stationName,b.id siteId,b.name siteName from T_EnvStation a left join T_Site b on a.site_id=b.id left join T_mid_RoleStation c on a.id=c.station_id where c.role_id=?";
    return this.jdbcTemplate.queryForList(sql, new Object[] { roleId });
  }
  
  public List<Map<String, Object>> getStationRanking(String roleId) {
    String sql = "select distinct a.id stationId,a.name stationName,isnull(CONVERT(VARCHAR,d.rectime,20),'-') time,e.online from T_EnvStation a left join T_Site b on a.site_id=b.id left join T_mid_RoleStation c on a.id=c.station_id left join T_realdata d on a.id=d.station_id left join T_EnvStationState e on a.stationState_id = e.id where c.role_id=? and a.enabled=1";
    return this.jdbcTemplate.queryForList(sql, new Object[] { roleId });
  }
  
  public List<Map<String, Object>> getAlarmRecord(String siteId, String stationId) {
    String sql = "select a.site_name siteName,a.station_name stationName,a.alarm_type alarmType,a.time,a.acontent content,a.person,a.alarm_step alarmStep from T_Alarm_Record a left join T_EnvStation b on a.stationId=b.id where a.stationid=? or b.site_id=?";
    return this.jdbcTemplate.queryForList(sql, new Object[] { stationId, siteId });
  }
  
  public List<Map<String, Object>> getAlarmRecordByType(String siteId, String stationId, String type) {
    String sql = "select a.site_name siteName,a.station_name stationName,a.alarm_type alarmType,a.time,a.acontent content,a.person,a.alarm_step alarmStep from T_Alarm_Record a left join T_EnvStation b on a.stationId=b.id where (a.stationid=? or b.site_id=?) and alarm_type=?";
    return this.jdbcTemplate.queryForList(sql, new Object[] { stationId, siteId, type });
  }
  
  public List<Map<String, Object>> getStationLevel(String stationId) {
    String sql = "select a.project_id projectId,b.name projectName,isnull(c.value,0) value,b.unit from T_Mid_ProjectStation a left join T_EnvProject b on a.project_id=b.id left join (select value,project_id from T_RealData where station_id=?) c on a.project_id = c.project_id where a.station_id=?";
    return this.jdbcTemplate.queryForList(sql, new Object[] { stationId, stationId });
  }
}